<?php

namespace app\admin\model\oil;

use think\Model;


class WorkOrder extends Model
{

    

    

    // 表名
    protected $name = 'oil_work_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'visittime_text',
        'status_text',
        'need_departments_text',
        'log_text',
    ];

    public function getNeedDepartmentsList()
    {
        return [
            'finance'=>'财务部',
            'invoice'=>'发票部',
            'technical'=>'技术部',
            'store'=>'油站部',
            'customer'=>'客服部',
        ];
    }

    public function getVisittimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['visittime']) ? $data['visittime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setVisittimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getStatusTextAttr($value, $data)
    {
        return $data['status']==1?"已处理":"待处理";
    }
    public function getNeedDepartmentsTextAttr($value, $data)
    {
        $list=$this->getNeedDepartmentsList();
        $value_arr=[];
        $arr=[];
        $val='';
        if($data['need_departments']){
            $value_arr=explode(',',$data['need_departments']);
            foreach ($value_arr as $v){
                $arr[$v]=$list[$v];
            }
        }
        $val=implode('|',$arr);
        return $val;
    }
    public function getLogTextAttr($value, $data)
    {
        $log_text='';
        $logs=WorkOrderLog::where('work_order_id',$data['id'])->select();
        foreach ($logs as $v){
            $log_text.="回访内容：".$v['content']."|回访时间：".date("Y-m-d H:i:s", $v['visittime'])."|信息来源：".$v['oper_name'].$v['work_type_title']."\n";
        }
        return $log_text;
    }

}
